//
//  XLComposeToolbar.swift
//  XLVeibo
//
//  Created by macbook on 16/7/3.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit

//用去区分是哪个按钮点击
enum composeToolbarType:Int {
    //图片
    case Picture = 10
    // @
    case Mention = 20
    // #
    case Trend = 30
    // 表情
    case Emoticon = 40
    // +
    case Add = 50
}

class XLComposeToolbar: UIView {
    
    
    var emotionBtn : UIButton?
    //切换键盘
    var isEmoticon:Bool = false{
    
        didSet{
            
            //显示键盘
            if isEmoticon {
                
             emotionBtn?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
                emotionBtn?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }else{
            //显示笑脸
                emotionBtn?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
                emotionBtn?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
                
            }
         }
      }
    
    

    //告知外界点击了那个按钮
    var colose:((type:composeToolbarType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
    
        
        let pictureBtn = addChildButton("compose_toolbar_picture", composeType: .Picture)
        let mentionBtn = addChildButton("compose_mentionbutton_background", composeType: .Mention)
        let trendBtn = addChildButton("compose_trendbutton_background", composeType: .Trend)
         emotionBtn = addChildButton("compose_emoticonbutton_background", composeType: .Emoticon)
        let addBtnBtn = addChildButton("compose_add_background", composeType: .Add)
        
        pictureBtn.snp_makeConstraints { (make) in
            
            make.left.top.bottom.equalTo(self)
            make.width.equalTo(mentionBtn)
            
        }
        
        mentionBtn.snp_makeConstraints { (make) in
            
            make.top.bottom.equalTo(self)
            make.left.equalTo(pictureBtn.snp_right)
            make.width.equalTo(trendBtn)
          }
        
        trendBtn.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(mentionBtn.snp_right)
            make.width.equalTo(emotionBtn!)
          }

        emotionBtn!.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(trendBtn.snp_right)
            make.width.equalTo(addBtnBtn)
         }

        addBtnBtn.snp_makeConstraints { (make) in
            make.top.bottom.right.equalTo(self)
            make.left.equalTo(emotionBtn!.snp_right)
         }
        
        
        
        
    }
    
    
    //提供公共方法创建按钮
    func addChildButton(imageName:String,composeType:composeToolbarType)->UIButton{
    
        let button = UIButton()
        //枚举值，转换成int  枚举值.rawValue == Int
        button.tag = composeType.rawValue
        
        //添加点击事件
        button.addTarget(self, action:#selector(XLComposeToolbar.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        addSubview(button)
        return button
    }
    
    
    //按钮方法的实现
    func buttonClick(button:UIButton){
    
        colose?(type:composeToolbarType(rawValue:button.tag)!)
    }
    
}











